#define DEBUG
#include <cstdio>
#define MAXN 600
#define MAXM 100000

using namespace std;

int mods;

class Graph {
public:
  int n, m, ind[MAXN+1], to[MAXM+1], link[MAXM+1], in[MAXN+1], out[MAXN+1];

  void addSide(int a, int b) {
    m++;
    to[m]=b;
    link[m]=ind[a];
    ind[a]=m;
    out[a]++, in[b]++;
  }

  void doACol(int s, long long *ans) {
    static long long f[MAXN+1];
    static int que[MAXN+1], tin[MAXN+1];
    int h=0, t=0;
    for (int i=1; i<=n; i++) {
      f[i]=0;
      tin[i]=in[i];
      if (!in[i]) {
	que[++t]=i;
      }
    }
    f[s]=1;
    for (; h<t; ) {
      h++;
      for (int i=ind[que[h]]; i; i=link[i]) {
	f[to[i]]=(f[to[i]]+f[que[h]])%mods;
	tin[to[i]]--;
	if (!tin[to[i]]) {
	  que[++t]=to[i];
	}
      }
    }
    for (int i=1, p=0; i<=n; i++) {
      if (!out[i]) {
	ans[++p]=f[i];
      }
    }
  }

  void makeMatrix(long long mat[MAXN+1][MAXN+1]) {
    int p=0;
    for (int i=1; i<=n; i++) {
      if (!in[i]) {
	p++;
	doACol(i, mat[p]);
      }
    }
    mat[0][0]=p;
  }
};

long long qpower(long long a, int n) {
  long long ret=a, s=1;
  for (; n>1; n/=2) {
    if (n%2) {
      s = s*ret%mods;
    }
    ret = ret*ret%mods;
  }
  return ret*s%mods;
}

long long calcDeterminant(long long mat[MAXN+1][MAXN+1]) {
  long long ans=1;
  for (int i=1; i<=mat[0][0]; i++) {
    int p;
    for (p=i; p<=mat[0][0] && !mat[p][i]; p++);
    if (p>mat[0][0]) {
      continue;
    }
    if (p!=i) {
      for (int j=1; j<=mat[0][0]; j++) {
	long long temp;
	temp=mat[i][j], mat[i][j]=mat[p][j], mat[p][j]=temp;
      }
      ans = ans*(mods-1)%mods;
    }

    for (int j=i+1; j<=mat[0][0]; j++) {
      long long t=mat[j][i]*qpower(mat[i][i], mods-2)%mods;
      for (int k=i; k<=mat[0][0]; k++) {
	mat[j][k] = (mat[j][k]+mods-t*mat[i][k]%mods)%mods;
      }
    }
  }

  for (int i=1; i<=mat[0][0]; i++) {
    ans = ans*mat[i][i]%mods;
  }
  return ans;
}

int main() {
#ifdef DEBUG
  freopen("4378.in", "r", stdin);
  freopen("4378.out", "w", stdout);
#endif

  static Graph g;
  static long long mat[MAXN+1][MAXN+1];
  int n, m;
  scanf("%d %d %d", &n, &m, &mods);
  g.n=n;
  for (int i=1; i<=m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    g.addSide(a, b);
  }
  g.makeMatrix(mat);

  printf("%lld", calcDeterminant(mat));

  fclose(stdin);
  fclose(stdout);
  return 0;
}
